package one.stand.controller;

import com.github.pagehelper.PageInfo;
import one.stand.model.CompanyCouponModel;
import one.stand.model.ResultModel;
import one.stand.request.CompanyCouponRequest;
import one.stand.request.CouponStateRequest;
import one.stand.request.CoupponRecordsRequest;
import one.stand.response.CompanyCouponResponse;
import one.stand.response.UserCouponResponse;
import one.stand.util.DateUtil;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import javax.websocket.server.PathParam;
import java.util.Date;

/**
 * 公司优惠券
 * @author: QIK
 * @CreateDate: 2019/6/24 15:42
 */
@RestController
@RequestMapping("company/coupon")
public class CompanyCouponController extends BaseController {

    /**
     * 创建优惠券
     * @param request
     * @return
     */
    @PostMapping("add")
    public  ResultModel<Boolean> add(@RequestBody  @Valid CompanyCouponRequest request){

        ResultModel resultModel = checkTime(request,false);
        if(resultModel == null){
            return companyCouponService.add(request);
        }
        return resultModel;
    }

    /**
     * 日期范围检查
     * @param request
     * @return
     */
    private ResultModel checkTime(CompanyCouponRequest request,boolean isUpdate){
        if(!isUpdate){
            //开始时间不能小于当前时间
            if(request.getGrantStartTime().getTime() < DateUtil.getStartTimeOfDay()){
                return ResultModel.fail("发放开始时间不能小于当前时间");
            }

            if(request.getEffectiveStartTime().getTime() < DateUtil.getStartTimeOfDay()){
                return ResultModel.fail("有效开始时间不能小于当前时间");
            }
        }

        if(request.getEffectiveStartTime().getTime() < request.getGrantStartTime().getTime() ){
            return ResultModel.fail("有效开始时间不能大于发放开始时间");
        }

        if(request.getEffectiveStartTime().getTime() > request.getEffectiveEndTime().getTime() ){
            return ResultModel.fail("有效开始时间不能大于结束时间");
        }

        if(request.getGrantStartTime().getTime() > request.getGrantEndTime().getTime() ){
            return ResultModel.fail("发放开始时间不能大于结束时间");
        }

        return null;
    }

    /**
     * 修改优惠券
     * @param request
     * @return
     */
    @PostMapping("upd")
    public ResultModel<Boolean> upd(@RequestBody  @Valid CompanyCouponRequest request){
        ResultModel resultModel = checkTime(request,true);
        if(resultModel == null){
            return companyCouponService.upd(request,request.getCouponId());
        }
        return resultModel;
    }

    @PostMapping("updState")
    public ResultModel<Boolean> updState(@RequestBody @Valid CouponStateRequest request){
        return companyCouponService.updState(request.getPubState(),request.getCouponId());
    }

    /**
     * 查询单个优惠券
     * @param request
     * @return
     */
    @PostMapping("findCompanyCouponById")
    public ResultModel<CompanyCouponResponse> findCompanyCouponById(@RequestBody CompanyCouponRequest request){
        return companyCouponService.findCompanyCouponById(request);
    }

    /**
     * 删除优惠券
     * @return
     */
    @PostMapping("del")
    public  ResultModel<Boolean> del(@RequestBody @Valid CouponStateRequest request){
        return companyCouponService.del(request.getCouponId());
    }

    /**
     * 获取优惠券列表
     * @param request
     * @return
     */
    @PostMapping("crmPcPageList")
    public ResultModel<PageInfo<CompanyCouponResponse>> crmPcPageList(@RequestBody CompanyCouponRequest request){
        return companyCouponService.crmPcPageList(request);
    }


    /**
     * 查询优惠券领取记录
     * @param request
     * @return
     */
    @PostMapping("coupponRecords")
    public ResultModel<PageInfo<UserCouponResponse>> coupponRecords(@RequestBody CoupponRecordsRequest request){
        return companyCouponService.coupponRecords(request);
    }

}
